﻿/*
 *   Copyright 2011 Peter Crossley
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *  ----------------------------------------------------------------------------
 *  File:       SysprepSettings.cs
 *  Author:     Peter Crossley
 *  ----------------------------------------------------------------------------
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UEC;
namespace UEC.Modules.Sysprep
{
    public partial class SysprepSettings : UserControl
    {
        private SysprepService service;
        private CloudManager manager;
        public SysprepSettings(SysprepService service)
        {
            this.service = service;
            this.manager = service.Manager;
            service.Manager.SaveSettingsEvent += new CloudManager.SaveSettingsHandler(SaveSettings);
            service.Manager.LoadSettingsEvent += new CloudManager.LoadSettingsHandler(LoadSettings);

            InitializeComponent();
            LoadSettings();

            this.txtTemplateLocation.TextChanged += new System.EventHandler(manager.SettingsChanged);

        }

        protected void LoadSettings(object sender=null, CloudManager.LoadEventArgs e=null)
        {
            if (e != null)
            {
                if (e.Reset)
                {
                    manager.DeleteKeyStore(SysprepService.SYSPREP_SETTINGS);
                }
            }
            txtTemplateLocation.Text = (string)manager.GetValue(SysprepService.SYSPREP_SETTINGS, "TemplateLocation","");
        }

        protected void SaveSettings(object sender, EventArgs e)
        {
            manager.SetValue(SysprepService.SYSPREP_SETTINGS, "TemplateLocation", txtTemplateLocation.Text);
        }

        private void btnGeneralize_Click(object sender, EventArgs e)
        {
            service.Manager.OnSaveSettings();
            if( String.IsNullOrEmpty(txtTemplateLocation.Text)) {
                MessageBox.Show(Properties.Resources.InvalidSysprepTemplate, Properties.Resources.SystemMessageTitle);
                return;
            }
            service.AuditSysprep();
            service.Manager.OnCloudManagerClose();
        }

        private void btBrowseTemplate_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.Title = "Load Sysprep Template";
            ofd.Filter = "XML Files (*.xml)|*.xml";
            if (DialogResult.OK == ofd.ShowDialog())
            {
                txtTemplateLocation.Text = ofd.FileName;
            }
        }

    }
}
